package com.berwin.bmfb.views;

import com.berwin.bmfb.entity.BMChar;
import com.berwin.bmfb.entity.BMFont;
import com.berwin.bmfb.utility.Utility;

import javax.swing.*;
import java.awt.*;

public class AttributesView extends BasePanel {

    private BMFont bmFont = null;
    private BMChar bmChar = null;

    private JTextField tfLetter = null;
    private JSpinner spX = null;
    private JSpinner spY = null;
    private JSpinner spWidth = null;
    private JSpinner spHeight = null;
    private JSpinner spXOffset = null;
    private JSpinner spYOffset = null;
    private JSpinner spAdvance = null;

    private class AttrImage extends JPanel {

        private BMFont bmFont = null;
        private BMChar bmChar = null;

        public AttrImage(BMFont bmFont) {
            this.bmFont = bmFont;
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            if (this.bmChar == null) {
                return;
            }
            g.drawImage(this.bmChar.getImage(), 0, 0, this.bmChar.getWidth(), this.bmChar.getHeight(), 0, 0, this.bmChar.getWidth(), this.bmChar.getHeight(), null);
        }

        public void updateBMChar(BMChar bmChar) {
            this.bmChar = bmChar;
            this.setPreferredSize(new Dimension(this.bmChar.getWidth(), this.bmChar.getHeight()));
            this.repaint();
        }
    }

    private AttrImage attrImage = null;

    public AttributesView(MainView mainView, BMFont bmFont) {
        super(mainView);
        this.bmFont = bmFont;

        this.setLayout(new BorderLayout());
        String names[] = {"字符", "x", "y", "width", "height", "xOffset", "yOffset", "advance"};

        JPanel pnlCenter = new JPanel();
        pnlCenter.setLayout(new GridLayout(names.length, 2));
        this.add(pnlCenter, BorderLayout.CENTER);

        // char
        this.tfLetter = new JTextField(6);
        this.tfLetter.setHorizontalAlignment(JTextField.RIGHT);
        // x,y
        SpinnerModel model = new SpinnerNumberModel(0, -1000, 1000, 1);
        this.spX = new JSpinner(model);
        this.spX.setEnabled(false);
        model = new SpinnerNumberModel(0, -1000, 1000, 1);
        this.spY = new JSpinner(model);
        this.spY.setEnabled(false);
        // width,height
        model = new SpinnerNumberModel(0, -1000, 1000, 1);
        this.spWidth = new JSpinner(model);
        this.spWidth.setEnabled(true);
        model = new SpinnerNumberModel(0, -1000, 1000, 1);
        this.spHeight = new JSpinner(model);
        this.spHeight.setEnabled(true);
        // xOffset,yOffset
        model = new SpinnerNumberModel(0, -1000, 1000, 1);
        this.spXOffset = new JSpinner(model);
        model = new SpinnerNumberModel(0, -1000, 1000, 1);
        this.spYOffset = new JSpinner(model);
        // advance
        model = new SpinnerNumberModel(0, -1000, 1000, 1);
        this.spAdvance = new JSpinner(model);


        JComponent components[] = {
                this.tfLetter,
                this.spX,
                this.spY,
                this.spWidth,
                this.spHeight,
                this.spXOffset,
                this.spYOffset,
                this.spAdvance,
        };

        for (int i = 0; i < components.length; i++) {
            pnlCenter.add(new JLabel(names[i]));
            pnlCenter.add(components[i]);
        }

        this.tfLetter.getDocument().addDocumentListener(this);
        this.spX.addChangeListener(this);
        this.spY.addChangeListener(this);
        this.spWidth.addChangeListener(this);
        this.spHeight.addChangeListener(this);
        this.spXOffset.addChangeListener(this);
        this.spYOffset.addChangeListener(this);
        this.spAdvance.addChangeListener(this);

        JPanel pnlSouth = new JPanel();
        this.add(pnlSouth, BorderLayout.SOUTH);
        pnlSouth.add(new JLabel());

        // 字符预览
        this.attrImage = new AttrImage(this.bmFont);
        this.attrImage.setPreferredSize(new Dimension(100, 100));
        pnlSouth.add(this.attrImage);
    }

    @Override
    public void onUpdate() {
//        tfLetter.setText(this.bmChar.getLetter());
//        spX.setValue(this.bmChar.getX());
//        spY.setValue(this.bmChar.getY());
//        spWidth.setValue(this.bmChar.getWidth());
//        spHeight.setValue(this.bmChar.getHeight());
//        spXOffset.setValue(this.bmChar.getxOffset());
//        spYOffset.setValue(this.bmChar.getyOffset());
//        spAdvance.setValue(this.bmChar.getxAdvance());
//        // image
//        this.attrImage.updateBMChar(bmChar);
    }


    @Override
    public void onSelectLetter(int idx) {
        if (idx < 0 || idx >= this.bmFont.getBmChars().size())
            return;
        this.isFromSelected = true;
        this.giveUpListenValueChange = true;
        this.bmChar = this.bmFont.getBmChars().get(idx);
        tfLetter.setText(this.bmChar.getLetter());
        spX.setValue(this.bmChar.getX());
        spY.setValue(this.bmChar.getY());
        spWidth.setValue(this.bmChar.getWidth());
        spHeight.setValue(this.bmChar.getHeight());
        spXOffset.setValue(this.bmChar.getxOffset());
        spYOffset.setValue(this.bmChar.getyOffset());
        spAdvance.setValue(this.bmChar.getxAdvance());
        // image
        this.attrImage.updateBMChar(bmChar);
        this.isFromSelected = false;
        this.giveUpListenValueChange = false;
        this.onValueChanged();
        this.repaint();
    }

    @Override
    public void onValueChanged() {
        if (this.isFromSelected || this.bmChar == null)
            return;
        this.bmChar.setLetter(tfLetter.getText());
        this.bmChar.setX((Integer) this.spX.getValue());
        this.bmChar.setY((Integer) this.spY.getValue());
        this.bmChar.setWidth((Integer) this.spWidth.getValue());
        this.bmChar.setHeight((Integer) this.spHeight.getValue());
        this.bmChar.setxOffset((Integer) this.spXOffset.getValue());
        this.bmChar.setyOffset((Integer) this.spYOffset.getValue());
        this.bmChar.setxAdvance((Integer) this.spAdvance.getValue());
        this.mainView.handleUpdate();
    }

    @Override
    public void onBMFontUpdated(BMFont bmFont) {
        this.bmFont = bmFont;
        this.giveUpListenValueChange = true;
        if (this.bmFont.getBmChars().size() <= 0) {
            tfLetter.setText("");
            spX.setValue(0);
            spY.setValue(0);
            spWidth.setValue(0);
            spHeight.setValue(0);
            spXOffset.setValue(0);
            spYOffset.setValue(0);
            spAdvance.setValue(0);
//            this.remove(this.attrImage);
//            this.attrImage = new AttrImage(this.bmFont);
//            this.add(this.attrImage);
            return;
        }
        this.bmChar = this.bmFont.getBmChars().get(0);

        tfLetter.setText(this.bmChar.getLetter());
        spX.setValue(this.bmChar.getX());
        spY.setValue(this.bmChar.getY());
        spWidth.setValue(this.bmChar.getWidth());
        spHeight.setValue(this.bmChar.getHeight());
        spXOffset.setValue(this.bmChar.getxOffset());
        spYOffset.setValue(this.bmChar.getyOffset());
        spAdvance.setValue(this.bmChar.getxAdvance());
        // image
        this.attrImage.updateBMChar(bmChar);
        this.giveUpListenValueChange = false;
        this.repaint();
    }
}
